import { Canvas } from "leaflet";

export function canvasExtend() {
    Canvas.include({
        _fillStroke: function (ctx, layer) {
            const options = layer.options;

            if (options.fill) {
                ctx.globalAlpha = options.fillOpacity;
                ctx.fillStyle = options.fillColor || options.color;

                ctx.fill(options.fillRule || "evenodd");
            }

            if (options.stroke && options.weight !== 0) {
                if (ctx.setLineDash) {
                    ctx.setLineDash(
                        (layer.options && layer.options._dashArray) || []
                    );
                }

                ctx.globalAlpha = options.opacity;
                ctx.lineWidth = options.weight;
                ctx.strokeStyle = options.color;
                ctx.lineCap = options.lineCap;
                ctx.lineJoin = options.lineJoin;
                ctx.stroke();
            }

            if (
                options.hasOwnProperty("render") &&
                typeof options.render === "function"
            ) {
                ctx.save(); // so we can remove the clipping
                ctx.clip();
                const bounds = layer._rawPxBounds;
                const size = bounds.getSize();
                // const pattern = ctx.createPattern(img, "repeat");
                ctx.fillStyle = options.render(ctx, size, layer);

                ctx.fillRect(bounds.min.x, bounds.min.y, size.x, size.y);
                ctx.restore();
            }
        },
    });
}
